<?php
/**
 * 短信下行服务
 */
class Util_SMSYP {

    public static function send($mobile, $text)
    {
        $apikey = "f2bd55d0c8dded6f517b1073087b3ce2"; //请用自己的apikey代替
        $url="http://yunpian.com/v1/sms/send.json";
        
        $encoded_text = urlencode("$text");
        $mobile = urlencode("$mobile");
        $post_string="apikey=$apikey&text=$encoded_text&mobile=$mobile";
        return self::sock_post($url, $post_string);
    }

    
    /**
    * url 为服务的url地址
    * query 为请求串
    */
    public static function  sock_post($url,$query){
        $data = "";
        $info=parse_url($url);
        $fp=fsockopen($info["host"],80,$errno,$errstr,30);
        if(!$fp){
            return $data;
        }
        $head="POST ".$info['path']." HTTP/1.0\r\n";
        $head.="Host: ".$info['host']."\r\n";
        $head.="Referer: http://".$info['host'].$info['path']."\r\n";
        $head.="Content-type: application/x-www-form-urlencoded\r\n";
        $head.="Content-Length: ".strlen(trim($query))."\r\n";
        $head.="\r\n";
        $head.=trim($query);
        $write=fputs($fp,$head);
        $header = "";
        while ($str = trim(fgets($fp,4096))) {
            $header.=$str;
        }
        while (!feof($fp)) {
            $data .= fgets($fp,4096);
        }
        return $data;
    }
    
}
?>
